<template>
  <div class="mod-config">
    <basic-container>

      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button
            v-if="permissions.goods_category_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)"
            >新增</el-button
          >
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table 
          :data="dataList" 
          row-key="id" 
          border 
          v-loading="dataListLoading" 
          :tree-props="{ children: 'children' }">

          <el-table-column
            prop="name"
            :show-overflow-tooltip="true"
            label="分类名称"
          >
          </el-table-column>
          <el-table-column
            prop="weight"
            header-align="center"
            align="center"
            :show-overflow-tooltip="true"
            label="排序"
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            label="创建时间"
          >
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-plus"
                @click="addOrUpdateHandle(false, scope.row.id)"
                v-if="permissions.goods_category_add"
                >新增
              </el-button>

              <el-button
                v-if="permissions.goods_category_edit"
                type="text"
                size="small"
                icon="el-icon-edit"
                @click="addOrUpdateHandle(true, scope.row.id)"
                >修改</el-button
              >
              <el-button
                v-if="permissions.goods_category_del"
                type="text"
                size="small"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 弹窗, 新增 / 修改 -->
      <EditForm
        v-if="visible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :visible.sync="visible"
      ></EditForm>
    </basic-container>
  </div>
</template>

<script>
import { fetchTree, delObj } from "@/api/shop_goods/category";
import EditForm from "./edit";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
      },
      dataList: [],

      dataListLoading: false,
      visible: false,
    };
  },
  components: {
    EditForm,
  },
  created() {
    this.getDataList();
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    /**
     * 获取数据列表
     */
    getDataList() {
      this.dataListLoading = true;
      fetchTree().then((response) => {
        this.dataList = response.data.data;
      });
      this.dataListLoading = false;
    },

    /**
     * 新增 / 修改
     */
    addOrUpdateHandle(isEdit, id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id);
      });
    },

    /**
     * 删除
     */
    deleteHandle(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getDataList();
        })
        .catch(() => {});
    },
  },
};
</script>
